package com.itheima.api.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.user.UmsResourceMapper;
import com.itheima.vo.domain.TableDataInfo;
import com.itheima.vo.pojo.UmsResource;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.checkerframework.checker.units.qual.Angle;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
 *权限表业务层实现类
 */
@DubboService
public class UmsResourceServiceImpl extends ServiceImpl<UmsResourceMapper, UmsResource> implements UmsResourceService {

    @Autowired
    private  UmsResourceMapper umsResourceMapper;

    /*
      分页模糊查询后台资源
       */
    @Override
    public TableDataInfo queryPage(Integer categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword) {
        Page<UmsResource> page = new Page<>(pageNum,pageSize);
        TableDataInfo tableDataInfo = new TableDataInfo();
        LambdaQueryWrapper<UmsResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(urlKeyword),UmsResource::getUrl,urlKeyword);
        queryWrapper.like(StringUtils.isNotEmpty(nameKeyword),UmsResource::getName,nameKeyword);
        queryWrapper.eq(categoryId!=null,UmsResource::getCategoryId,categoryId);
         umsResourceMapper.selectPage(page, queryWrapper);
        List<UmsResource> list = page.getRecords();
        for (UmsResource umsResource : list) {
            umsResource.setCreateTime(LocalDateTime.now());
        }
        tableDataInfo.setList(list);
        tableDataInfo.setPageNum(pageNum);
        tableDataInfo.setPageSize(pageSize);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setTotalPage(page.getPages());
        return tableDataInfo;
    }
}
